// (已清理无效内容)
package com.example.tank.entity;

import java.awt.*;

public class Enemy {
    private int x;
    private int y;
    private int width = 40;
    private int height = 40;
    private int speed = 2;
    private int direction = 1; // 0-up, 1-right, 2-down, 3-left
    private int moveCounter = 0;
    private int shootCounter = 0;
    private static final int SHOOT_DELAY = 120; // frames between shots
    private static final int CHANGE_DIRECTION_TIME = 60; // frames
    // 添加面板边界常量
    private static final int PANEL_WIDTH = 800;
    private static final int PANEL_HEIGHT = 600;
    
    public Enemy(int x, int y) {
        // 确保初始位置在边界内
        this.x = Math.max(0, Math.min(x, PANEL_WIDTH - width));
        this.y = Math.max(0, Math.min(y, PANEL_HEIGHT - height));
    }

        // 以下方法用于支持GamePanel中的坦克移动和状态控制
    public int getSpeed() { return speed; }
    public void setDirection(int d) { this.direction = d; }
    public void setMoveCounter(int c) { this.moveCounter = c; }
    public void setX(int x) { this.x = x; }
    public void setY(int y) { this.y = y; }
    public int getMoveCounter() { return moveCounter; }
    public int getChangeDirectionTime() { return CHANGE_DIRECTION_TIME; }
    
    public boolean shouldShoot() {
        shootCounter++;
        if (shootCounter >= SHOOT_DELAY) {
            shootCounter = 0;
            return true;
        }
        return false;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getDirection() {
        return direction;
    }

    public void update() {
        int newX = x;
        int newY = y;
        
        // 计算新位置
        switch (direction) {
            case 0: // up
                newY -= speed;
                break;
            case 1: // right
                newX += speed;
                break;
            case 2: // down
                newY += speed;
                break;
            case 3: // left
                newX -= speed;
                break;
        }
        
        // 检查边界，如果超出边界则改变方向
        if (newX < 0 || newX + width > PANEL_WIDTH || newY < 0 || newY + height > PANEL_HEIGHT) {
            // 如果超出边界，改变方向而不是移动
            direction = (int) (Math.random() * 4);
            moveCounter = 0;
        } else {
            // 如果在边界内，执行移动
            x = newX;
            y = newY;
        }
        
        // Change direction periodically or add some randomness
        moveCounter++;
        if (moveCounter >= CHANGE_DIRECTION_TIME || Math.random() < 0.02) {
            direction = (int) (Math.random() * 4);
            moveCounter = 0;
        }
    }
    
    public void draw(Graphics g) {
        // 绘制敌方坦克主体（更接近经典样式）
        g.setColor(Color.RED);
        
        // 主体矩形
        g.fillRect(x + 5, y + 5, width - 10, height - 10);
        
        // 绘制坦克履带
        g.setColor(new Color(139, 0, 0)); // 深红色履带
        g.fillRect(x, y, 5, height);       // 左履带
        g.fillRect(x + width - 5, y, 5, height); // 右履带
        g.fillRect(x, y, width, 5);        // 上履带
        g.fillRect(x, y + height - 5, width, 5); // 下履带
        
        // 绘制炮塔
        g.setColor(Color.DARK_GRAY);
        g.fillRect(x + width/2 - 8, y + height/2 - 8, 16, 16);
        
        // 绘制炮管（根据方向）
        g.setColor(Color.BLACK);
        switch (direction) {
            case 0: // up
                g.fillRect(x + width/2 - 2, y - 8, 4, 12);
                break;
            case 1: // right
                g.fillRect(x + width, y + height/2 - 2, 12, 4);
                break;
            case 2: // down
                g.fillRect(x + width/2 - 2, y + height, 4, 12);
                break;
            case 3: // left
                g.fillRect(x - 12, y + height/2 - 2, 12, 4);
                break;
        }
    }
    
    public Rectangle getBounds() {
        return new Rectangle(x, y, width, height);
    }
    
    public boolean isHit(Rectangle bullet) {
        return getBounds().intersects(bullet);
    }
}